
;(function () {

    //链接AJAX提交数据
    if ($('a.J_ajax_link').length) {
            $('.J_ajax_link').on('click', function (e) {
                e.preventDefault();
                var $_this = this,
                    $this = $($_this),
                    href = $this.prop('href'),
                    datas = $this.data('datas'),
                    msg=$this.data('msg'),
                    delmsg=$this.data('delmsg'),
                    notemsg=(msg!=null)?msg:'系统提示';

                //如果是删除则提示
                if(delmsg !== undefined){
                    if(!confirm(delmsg)){
                        return false;
                    }
                }
                //分割提交数据
                if(datas.indexOf("~"))
                {
                    datas=datas.replace(eval("/~/gi"),'/');
                    datas=datas.replace(eval("/,/gi"),'/');
                }else{
                    datas=datas.replace(',','/')
                }
                var newhref=href+datas;

                $.getJSON(newhref).done(function (data) {
                    if (data.status == true) {
                        openMyDialog(data.info,notemsg);
                        if (data.url) {
                            setTimeout(function(){
                                location.href=data.url;
                            },2000);
                        } else {
                            setTimeout(function(){
                                reloadPage(window)
                            },2000);
                        }
                    } else if (data.status == false) {
                        openMyDialog(data.info,notemsg);
                    }
                });

            });
    }

    //不刷新替换文字操作
    $(".R_ajax_link").on('click',function(e){
        e.preventDefault();
        var $this = $(this),
            url = $this.attr('data-url'),
            datas = $this.attr('data-info'),
            linkurl='';
        //分割提交数据
        if(datas.indexOf("~"))
        {
            datas=datas.replace(eval("/~/gi"),'/');
            datas=datas.replace(eval("/,/gi"),'/');
        }else{
            datas=datas.replace(',','/')
        }
        linkurl=url+datas;
        $.getJSON(GM.siteUrl + linkurl).done(function (data) {
            if (data.status == true) {
                //$this.html(data.info);
                //$this.attr('data-url',data.url);
				window.location.reload();
            } else if (data.status == false) {
                $.tipMessage(data.info, 1, 3000);
            }
        });
    });

    //提交留言
    $("#sendMsg_btn").on('click',function(){
        var $infoid=$("#infoid"),
            $type=$("#msgType"),
            $content=$("#content");
        var num = $content.attr('data-len');
        if ($content.val().length<num) {
            $.tipMessage('内容不能少于'+num+'个字', 1, 3000);
            $content.val('').focus();
            return false;
        }
        $.ajax({
            type: "POST",
            global: false,
            url: GM.siteUrl + "index.php?m=Home&c=Do&a=sendmsg",
            data: {
                infoid: $infoid.val(),
                type: $type.val(),
                content: $content.val()
            },
            dataType: "json",
            success: function (data) {
                if(data.status){
                    $.tipMessage(data.info, 1, 2000,0,function(){
                        fleshPage();
                    });
                }else{
                    $.tipMessage(data.info, 1, 2000);
                    return false;
                }
            },
            error: function () {
                alert("数据执行错误！");
                return false;
            }

        });

    });


    //留言回复按钮
/*    $(".openReply_btn").on('click',function(e){
        e.preventDefault();
        if ($(this).hasClass('cancel-reply')) {
            $(this).removeClass('cancel-reply').html("回复");
            $(".list-reply").remove();
        }else{
            var msgid=$(this).attr('data-infoid'),
                touid=$(this).attr('data-touid'),
                type=$(this).attr('data-type'),
                reply=$(".list-reply");
            if(reply.length>0){
                $(".b-huiying__btn").removeClass('cancel-reply').html("回复");
                reply.remove();
            }
            $(this).addClass('cancel-reply').html("取消");
            var str="<ul class='list-reply'>";
            str+='<form onsubmit="return false;">';
            str+='<li><input type="hidden" id="infoid" value="'+msgid+'" /><input type="hidden" id="touserid" value="'+touid+'" /><input type="hidden" id="msgType" value="'+type+'" /> </li>';
            str+='<li><textarea id="rcontent" title="回复留言至少5个字! " required minlength="5" rows="2"></textarea><button class="M_Reply_btn">回复</button></li>';
            str+='</form>'
            str+="</ul>";
            $(this).parent().append(str);
        };
    });
*/
    $(".yf-huiying-send").on('click',function(e){
        e.preventDefault();
        if ($(this).hasClass('cancel-reply')) {
            $(this).removeClass('cancel-reply').html("回复");
            $(".yf-huiying-tempbox").remove();
        }else{
            var msgid = $(this).attr('data-infoid'),
                touid = $(this).attr('data-touid'),
                type = $(this).attr('data-type'),
                parent = $(this).attr('data-parent'),
                parent_id = $(this).attr('data-id'),
                user = $(this).attr('data-user'),
                reply = $(".yf-huiying-tempbox");
            if(reply.length>0){
                $(".yf-huiying-send").removeClass('cancel-reply').html("回复");
                reply.remove();
            }
       
            $(this).addClass('cancel-reply').html("取消");
       /*     var str="<ul class='list-reply'>";
            str+='<form onsubmit="return false;">';
            str+='<li><input type="hidden" id="infoid" value="'+msgid+'" /><input type="hidden" id="touserid" value="'+touid+'" /><input type="hidden" id="msgType" value="'+type+'" /> </li>';
            str+='<li><textarea id="rcontent" title="回复留言至少5个字! " required minlength="5" rows="2"></textarea><button class="M_Reply_btn">回复</button></li>';
            str+='</form>'
            str+="</ul>";*/
            var str = "<div class='yf-huiying-list yf-huiying-tempbox'>";
				str += "<div class='yf-huiying-box'>";
				str += "<form onsubmit='return false;'>";
				str += "<input type='hidden' id='infoid' value='"+msgid+"' />";
				str += "<input type='hidden' id='touserid' value='"+touid+"' />";
				str += "<input type='hidden' id='msgType' value='"+type+"' />";
				str += "<input type='hidden' id='parent_id' value='"+parent_id+"' />";
				str += "<textarea id='rcontent' class='yf-huiying-textarea' title='回复留言至少3个字!' required data-len='3' rows='2' placeholder='@"+user+"，我也说一句...'></textarea>";
				str += "<button class='M_Reply_btn yf-huiying-btn'>回复</button>";
				str += "</form>";
				str += "</div>";
				str += "</div>";
            if(parent == 2)
            {
            	$(this).parent().parent().parent().find('.yf-huiying-child').append(str);
            }
            else if(parent == 4)
            {
            	//$(this).parent().parent().parent().parent().parent().append(str);
            	var ys = $(this).parent().parent().parent().parent();
            	$(str).insertAfter(ys);
            }
            $('.yf-huiying-textarea').focus();
        };
    });
    //留言回复内容
    $(".M_Reply_btn").live('click',function(e){
        e.preventDefault();
        var $infoid=$("#infoid"),
            $touid=$("#touserid"),
            $type=$("#msgType"),
            $parent = $('#parent_id'),
            $rcontent=$("#rcontent");
        var num = $rcontent.attr('data-len');
        if ($rcontent.val().length<num) {
            $.tipMessage('内容不能少于'+num+'个字', 1, 2000);
            $rcontent.val('').focus();
            return false;
        }
        $.ajax({
            type: "POST",
            global: false,
            url: GM.siteUrl + "index.php?m=Home&c=Do&a=replymsg",
            data: {
                infoid: $infoid.val(),
                touserid: $touid.val(),
                type: $type.val(),
                parent_id: $parent.val(),
                content: $rcontent.val()
            },
            dataType: "json",
            success: function (data) {
                if(data.status){
                 /*   $.tipMessage(data.info, 1, 2000,0,function(){
                        reloadPage(window);
                    });*/
                	 $.tipMessage(data.info, 1, 2000,0,function(){
                         fleshPage();
                     });
                }else{
                    $.tipMessage(data.info, 1, 2000);
                    return false;
                }
            },
            error: function () {
                alert("数据执行错误！");
                return false;
            }

        });
    });


    //删除留言
    $(".msgDel_btn").on('click',function(e){
        e.preventDefault();
        var $this = $(this),
            $url=$this.attr('data-url'),
            $datas = $this.attr('data-info');
            $datas=$datas.replace(',','/')
        $.getJSON(GM.siteUrl+$url+$datas).done(function (data) {
            if (data.status == true) {
                $.tipMessage(data.info, 1, 1000,0,function(){
                	fleshPage();
                });
                return false;
            } else if (data.status == false) {
                $.tipMessage(data.info, 1, 3000);
            }
        });

    });

    //列表点赞
    $(".R_ajax_dig").click(function(e){
        e.preventDefault();
        var $this = $(this),
            $url=$this.attr('data-url'),
            $datas = $this.attr('data-info'),
            $kind='';

        if($datas.indexOf("~"))
        {
            $datas=$datas.replace(eval("/~/gi"),'/');
            $datas=$datas.replace(eval("/,/gi"),'/');
        }else{
            $datas=$datas.replace(',','/')
        }

        $.getJSON(GM.siteUrl+$url+$datas).done(function (data) {
            if (data.status == true) {
                if($this.find('i').hasClass('undig')){
                    $this.find('i').removeClass('undig').addClass('digged');
                    $kind='add';
                }else{
                    $this.find('i').removeClass('digged').addClass('undig');
                    $kind='cancel';
                }
                if($kind=='add'){
                    $this.attr('data-url','do/canceldig/');
                }else if($kind=='cancel'){
                    $this.attr('data-url','do/adddig/');
                }
                return false;
            } else if (data.status == false) {
                $.tipMessage(data.info, 1, 3000);
            }
        });

    });

    //显示操作信息且刷新页面
    $(".R_ajax_trip").click(function(e){
        e.preventDefault();
        var $this = $(this),
            $url=$this.attr('data-url'),
            $datas = $this.attr('data-info');
		var $lv = $datas.split(',');
			if(($lv[1]+0) == 0){
				$.tipMessage("只有会员才能进行此操作，请登录！", 1, 1000);
			}
			
        if($datas.indexOf("~"))
        {	
            $datas=$datas.replace(eval("/~/gi"),'/');
            $datas=$datas.replace(eval("/,/gi"),'/');
        }else{
            $datas=$datas.replace(',','/')
        }
		
		
        $.getJSON(GM.siteUrl+$url+$datas).done(function (data) {
            if (data.status == true) {
                $.tipMessage(data.info, 1, 1000,0,function(){
                    fleshPage();
                });
                return false;
            } else if (data.status == false) {
                $.tipMessage(data.info, 1, 1000);
            }
        });
    });

    //提交作品
    $('button.Ajax_submit_btn').on('click', function (e) {
        e.preventDefault();
        var btn = $(this),
            form = btn.parents('form.ajaxForm');
        form.ajaxSubmit({
            url: btn.data('action') ? btn.data('action') : form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
            dataType: 'json',
            beforeSubmit: function (arr, $form, options) {
                var text = btn.text();
                btn.text(text + '中...').prop('disabled', true).addClass('disabled');
            },
            success: function (data, statusText, xhr, $form) {
                var text = btn.text();
                btn.removeClass('disabled').text(text.replace('中...', '')).parent().find('span').remove();
                if(data.status == undefined){
                    $.tipMessage(data.info, 1, 3000);
                }
                if (data.status === 1) {
                    if(data.url){
                        $.tipMessage(data.info, 1, 2000,0,function(){
                            location.href = data.url;
                        });
                    }else{
                        $.tipMessage(data.info, 1, 2000,0,function(){
                            reloadPage(window);
                        });
                    }
                } else if (data.status === 0) {
                    $.tipMessage(data.info, 1, 3000);
                    btn.removeProp('disabled').removeClass('disabled');
                }
            }
        });

    });

    //上传头像
    $('#upuhead').die('click').live('change', function(e){
        e.preventDefault();
        var __this = $(this),
            form = __this.parents('form.ajaxForm');
        form.ajaxSubmit({
            url: form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
            dataType: 'json',
            success: function (data, statusText, xhr, $form) {
                if(data.status == undefined){
                    $.tipMessage(data.info, 1, 3000);
                }
                if (data.status === 1) {
                    $.tipMessage(data.info, 1, 2000,0,function(){
                        reloadPage(window);
                    });
                } else if (data.status === 0) {
                    $.tipMessage(data.info, 1, 3000);
                }
            }
        });
    })

    $("#loginSubmit").live('click',function () {
        var $backurl = $("#backurl").val();
        var $uname = $("#uname");
        var $password = $("#password");
        var $autoLogin = $('#autoLogin');
        var $errmsg = $('.errmsg');
        if ($uname.val().length<3) {
            $errmsg.html('请输入正确的账号！').show();
            $uname.val('').focus();
            return false;
        }  else if ($password.val().length < 6) {
            $errmsg.html('密码长度应大于6位！').show();
            $password.focus();
            return false;
        }

        if ($autoLogin.checked==true) {
            $autoLogin = 1;
        } else {
            $autoLogin = 0;
        }
        $errmsg.html("登录中，请稍后...").show();

        $.getJSON(GM.siteUrl + "index.php?m=User&c=Login&a=dologin", "username=" + escape($uname.val()) + "&password=" + escape($password.val()) + "&autoLogin=" + escape($autoLogin) + '&escape=1',
            function (data) {
                if(data.status){
                    $errmsg.html(data.info);

                    if ($backurl != undefined && $backurl != '') {
                        location.href = $backurl;
                    } else {
                        location.href = data.url;
                    }

                }else{
                    $errmsg.html(data.info);
                    return false;
                }

            });
        return false;
    });


})(jQuery);


